<?php

namespace App\Shop\Controllers;

use App\Admin\Forms\Service\HandleCancel;
use App\Admin\Forms\Service\HandleShop;
use App\Admin\Forms\Service\HandleWorker;
use App\Admin\Repositories\ServiceOrder;
use App\Models\ShopUser;
use App\Models\Worker;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\ServiceOrder as Model;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Contracts\Support\Renderable;

class ServiceOrderController extends AdminController
{

    /**
     * Make a grid builder.
     *
     */
    protected function grid()
    {
        $row = new Row;
        $tab = Tab::make();
        $m = request()->input('m',0);
        $shopUser = auth("shop")->user();
        $shop_id = $shopUser->id;
        $tab->addLink('已安排服务', url('shop/service-order')."?m=0", intval($m)==0);
        $count = Model::query()->where("shop_id",$shop_id)
            ->where("worker_id",0)->where("is_apply",0)->where("status",0)->count();
        $tab->addLink("待安排服务({$count})", url('shop/service-order')."?m=1", intval($m)==1);
        $count1 = Model::query()->where("shop_id",$shop_id)
            ->where("worker_id",">",0)->where("is_apply",0)->where("status",0)->count();
        $tab->addLink("待上门服务({$count1})", url('shop/service-order')."?m=2", intval($m)==2);
        $count2 = Model::query()->where("shop_id",$shop_id)->where("is_apply",1)->count();
        $tab->addLink("取消服务申请({$count2})", url('shop/service-order')."?m=3", intval($m)==3);
        $tab->addLink("已取消的服务", url('shop/service-order')."?m=4", intval($m)==4);
        $row->column(12,$tab);
        if ($m==0){
            $row->column(12,$this->order());
        }elseif ($m==1){
            $row->column(12,$this->code());
        }elseif ($m==2){
            $row->column(12,$this->wait());
        }elseif($m==3){
            $row->column(12,$this->apply());
        }else{
            $row->column(12,$this->cancel());
        }
        return Card::make($row);
    }

    protected function makeGrid(\Closure $callback): Grid
    {
        return Grid::make(new ServiceOrder(['user','worker','shop']), function (Grid $grid) use ($callback) {
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disableDeleteButton();
            $this->export($grid);
            $callback($grid);
        });
    }

    protected function export(Grid $grid){
        $titles = [
            'order_id' => '订单编号', 'nickname' => '昵称', 'title' => '服务名称','shop_name'=>'洗车门店',
            'worker_name'=>'洗车技师', 'plate_number'=>'车牌号','brand_name'=>'车牌类型','name'=>'客户姓名',
            'phone'=>'客户电话','amount'=>'支付金额','appoint_time'=>'预约时间','coupon_price'=>'优惠金额金额',
            'address'=>'收货地址','order_time'=>'下单时间','status_text'=>'订单状态','state_text'=>'完成状态',
            'arrive_time'=>'到达时间','finish_time'=>'完成时间'
        ];
        $grid->export()->rows(function ($rows) {
            $statusList = Model::getStatusList();
            foreach ($rows as $index => &$row) {
                $row['nickname'] = $row['user']?$row['user']['nickname']:"";
                $row['title'] = $row['service']?$row['service']['title']:"";
                $row['shop_name'] = $row['shop']?$row['shop']['name']:"";
                $row['worker_name'] = $row['worker']?$row['worker']['name']:"";
                $row['plate_number'] = $row['car']?$row['car']['plate_number']:"";
                $row['brand_name'] = $row['car']?$row['car']['brand_name']:"";
                $row['name'] = $row['car']?$row['car']['name']:"";
                $row['phone'] = $row['car']?$row['car']['phone']:"";
                $row['status_text'] = $statusList[$row['status']]??"";
                $row['state_text'] = $row['state']?"已完成":"未完成";
                $row['appoint_time'] = $row['appoint_time']?:"立即上门";
            }
            return $rows;
        })->titles($titles);
    }

    protected function code(){
        return $this->makeGrid(function (Grid $grid){
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $grid->model()->where("shop_id",$shop_id)->where("is_apply",0)
                ->where("worker_id",0)->where("status",0);
            $grid->column('order_id');
            $grid->column('user.nickname','用户昵称')->limit(8);
            $grid->column('service.title','服务名称')->limit(8);
            $grid->column('car.plate_number','车牌号')->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['color']})";
            });
            $grid->column('car.brand_name',"车辆品牌类型")->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['type']})";
            });
            $grid->column('car.name','客户姓名电话')->display(function ($value){
                $car = $this->car;
                return "{$value}({$car['phone']})";
            })->limit(10);
            $grid->column('amount');
            $grid->column('appoint_time')->display(function ($value){
                return $value?:"立即上门";
            });
            $grid->column('coupon_price');
            $grid->column('address')->limit(15);
            $grid->column('order_time');
            $grid->column('status','安排')->display(function (){
                return "安排洗车师傅";
            })->modal("安排洗车师傅",function (){
                return HandleWorker::make()->payload(['oid'=>$this->id]);
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->like('order_id');
                $filter->like('user.nickname','用户昵称');
                $filter->like('car->plate_number','车牌号');
                $filter->like('car->name','客户姓名');
                $filter->like('car->phone','客户电话');
                $filter->like('address','地址');
                $filter->between('order_time','下单时间')->date();
                $filter->equal('status')->select(Model::getStatusList());
            });

        });
    }

    /**
     * 取消申请
     * @return Grid
     */
    protected function apply(){
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("is_apply",1);
            $grid->column('order_id');
            $grid->column('user.nickname','用户昵称')->limit(8);
            $grid->column('service.title','服务名称')->limit(8);
            $grid->column('car.plate_number','车牌号')->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['color']})";
            });
            $grid->column('car.brand_name',"车辆品牌类型")->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['type']})";
            });
            $grid->column('car.name','客户姓名电话')->display(function ($value){
                $car = $this->car;
                return "{$value}({$car['phone']})";
            })->limit(10);
            $grid->column('amount');
            $grid->column('appoint_time')->display(function ($value){
                return $value?:"立即上门";
            });
            $grid->column('coupon_price');
            $grid->column('address')->limit(15);
            $grid->column('order_time');
            $grid->column('status','取消申请审核')->display(function (){
                return "取消申请审核";
            })->modal("取消申请审核",function (){
                return HandleCancel::make()->payload(['oid'=>$this->id]);
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->like('order_id');
                $filter->like('user.nickname','用户昵称');
                $filter->like('car->plate_number','车牌号');
                $filter->like('car->name','客户姓名');
                $filter->like('car->phone','客户电话');
                $filter->like('address','地址');
                $filter->between('order_time','下单时间')->date();
                $filter->equal('status')->select(Model::getStatusList());
            });
        });
    }

    /**
     * 待上门
     * @return Grid
     */
    protected function wait(){
        return $this->makeGrid(function (Grid $grid){
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $grid->model()->where("shop_id",$shop_id)->where("is_apply",0)
                ->where("worker_id",">",0)->where("status",0);
            $grid->column('order_id');
            $grid->column('user.nickname','用户昵称')->limit(8);
            $grid->column('service.title','服务名称')->limit(8);
            $grid->column('car.plate_number','车牌号')->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['color']})";
            });
            $grid->column('car.brand_name',"车辆品牌类型")->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['type']})";
            });
            $grid->column('car.name','客户姓名电话')->display(function ($value){
                $car = $this->car;
                return "{$value}({$car['phone']})";
            })->limit(10);
            $grid->column('amount');
            $grid->column('appoint_time')->display(function ($value){
                return $value?:"立即上门";
            });
            $grid->column('coupon_price');
            $grid->column('address')->limit(15);
            $grid->column('order_time');
            $grid->column('status','更换师傅')->display(function (){
                return "更换师傅";
            })->modal("更换师傅",function (){
                return HandleWorker::make()->payload(['oid'=>$this->id]);
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->like('order_id');
                $filter->like('user.nickname','用户昵称');
                $filter->like('car->plate_number','车牌号');
                $filter->like('car->name','客户姓名');
                $filter->like('car->phone','客户电话');
                $filter->like('address','地址');
                $filter->between('order_time','下单时间')->date();
                $filter->equal('status')->select(Model::getStatusList());
            });
        });
    }

    /**
     * 已取消的服务
     */
    protected function cancel(): Grid
    {
        return $this->makeGrid(function (Grid $grid){
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $grid->model()->where("shop_id",$shop_id)->where("is_apply",0)->where("status",4);
            $grid->column('order_id');
            $grid->column('user.nickname','用户昵称')->limit(8);
            $grid->column('service.title','服务名称')->limit(8);
            $grid->column('car.plate_number','车牌号')->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['color']})";
            });
            $grid->column('car.brand_name',"车辆品牌类型")->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['type']})";
            });
            $grid->column('car.name','客户姓名电话')->display(function ($value){
                $car = $this->car;
                return "{$value}({$car['phone']})";
            })->limit(10);
            $grid->column('amount');
            $grid->column('appoint_time')->display(function ($value){
                return $value?:"立即上门";
            });
            $grid->column('coupon_price');
            $grid->column('address')->limit(15);
            $grid->column('order_time');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->like('order_id');
                $filter->like('user.nickname','用户昵称');
                $filter->like('car->plate_number','车牌号');
                $filter->like('car->name','客户姓名');
                $filter->like('car->phone','客户电话');
                $filter->like('address','地址');
                $filter->between('order_time','下单时间')->date();
                $filter->equal('status')->select(Model::getStatusList());
            });
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function order()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->scrollbarX();
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $grid->model()->where("shop_id",$shop_id)->where("worker_id",">",0)
                ->where("is_apply",0);
            $grid->fixColumns(2, -2);
            $grid->column('order_id');
            $grid->column('worker.name','洗车技师')->limit(8);
//            $grid->column('shop.name','洗车门店')->limit(8);
            $grid->column('service.title','服务名称')->limit(8);
            $grid->column('user.nickname','用户昵称')->limit(8);
            $grid->column('address')->limit(15);
            $grid->column('car.plate_number','车牌号')->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['color']})";
            });
            $grid->column('car.brand_name',"车辆品牌类型")->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['type']})";
            });
            $grid->column('car.name','客户姓名电话')->display(function ($value){
                $car = $this->car;
                return "{$value}({$car['phone']})";
            })->limit(10);
            $grid->column('amount');
            $grid->column('appoint_time')->display(function ($value){
                return $value?:"立即上门";
            });
            $grid->column('coupon_price');
            $grid->column('order_time');
            $grid->column('arrive_time');
            $grid->column('finish_time');
            $grid->column('status')->using(Model::getStatusList())->label(['default','primary','info','success','danger']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->like('order_id');
                $filter->like('user.nickname','用户昵称');
//                $filter->equal('shop_id','所属门店')->select(ShopUser::query()->pluck("name","id"));
                $filter->equal('worker_id','洗车技师')->select(Worker::query()->pluck("name","id"));
                $filter->like('car->plate_number','车牌号');
                $filter->like('car->name','客户姓名');
                $filter->like('car->phone','客户电话');
                $filter->like('address','地址');
                $filter->between('order_time','下单时间')->date();
                $filter->equal('status')->select(Model::getStatusList());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ServiceOrder(), function (Show $show) {
            $show->field('id');
            $show->field('order_id');
            $show->field('payment_no');
            $show->field('user_id');
            $show->field('shop_id');
            $show->field('worker_id');
            $show->field('amount');
            $show->field('service');
            $show->field('service_id');
            $show->field('appoint_time');
            $show->field('coupon_id');
            $show->field('coupon_price');
            $show->field('address');
            $show->field('status');
            $show->field('order_time');
            $show->field('arrive_time');
            $show->field('finish_time');
            $show->field('infos');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ServiceOrder(['user','worker','shop']), function (Form $form) {
            $form->column(6,function (Form $form){
                $form->display('order_id');
                $form->display('user.nickname','用户昵称')->with(function ($value){
                    return $value."【用户ID:{$this->user_id}】";
                });
                $form->display('shop.name','门店名称')->with(function ($value){
                    return $value."【门店ID:{$this->shop_id}】";
                });
                $form->display('worker.name','技师师傅')->with(function ($value){
                    return $value."【技师ID:{$this->worker_id}】";
                });
                $form->display('amount');
                $form->display('service.title','服务名称')->with(function ($value){
                    return $value."【服务ID:{$this->service_id}】";
                });
                $form->display('appoint_time')->help("0:表示立即上门");

                $form->embeds("info1","确认时环车检查信息",function ($table){
                    $table->textarea("content","备注信息")->disable();
                    $table->multipleImage("images","图片信息")->disable();
                });
            });
            $form->column(6,function (Form $form){
                $form->display('coupon_price')->with(function ($value){
                    $coupon_id = $this->coupon_id?:0;
                    return $value."【优惠券ID:{$coupon_id}】";
                });
                $form->display('car.plate_number','车牌号');
                $form->display('car.name','客户姓名电话')->with(function ($value){
                    $phone = $this->car['phone'];
                    return $value."【{$phone}】";
                });
//                $form->display('car.phone','客户电话');
                $form->display('address');
                $form->display('status')->with(function ($value){
                    $statusList = Model::getStatusList();
                    return $statusList[$value];
                });
                $form->display('state','完成状态')->with(function ($value){
                    $stateList = ["未完成","已完成"];
                    return $stateList[$value];
                });

//                $form->radio('status')->options(Model::getStatusList())->default(0);
                $form->display('order_time');
                $form->display('arrive_time');
                $form->display('finish_time');
                $form->embeds("info2","完成时环车检查信息",function ($table){
                    $table->textarea("content","备注信息")->disable();
                    $table->multipleImage("images","图片信息")->disable();
                });
            });

        });
    }
}
